<?php

namespace App\Services\User;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Models\User\Address;
use App\Services\BaseService;


class AddressService extends BaseService
{
    /**
     * 获取地址列表
     * @param  int  $userId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getAddressListByUserId(int $userId)
    {
        return Address::query()
            ->where('user_id', $userId)
            ->where('deleted', 0)
            ->get();
    }

    /**
     * 获取用户地址
     * @param $userId
     * @param $addressId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getAddress($userId, $addressId)
    {
        return Address::query()
            ->where('user_id', $userId)
            ->where('id', $addressId)
            ->where('deleted', 0)
            ->first();
    }

    /**
     * 删除用户地址
     * @param $userId
     * @param $addressId
     * @return bool|mixed|void
     * @throws BusinessException
     */
    public function delete($userId, $addressId)
    {
        $address = $this->getAddress($userId, $addressId);
        if (is_null($address)){
            $this->throwBusinessExceptiong(CodeResponse::PARAM_ILLEGAL);
        }
        $res = $address->delete();
         if($res){
             return $res;
         }
        $this->throwBusinessExceptiong(CodeResponse::UPDATED_FAIL);
    }
}
